package com.kidedu.action;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.kidedu.entity.KOrder;
import com.kidedu.entity.KStudent;
import com.kidedu.entity.dto.KOrderDTO;
import com.kidedu.service.IKOrderService;
import com.kidedu.util.Pagination;
import com.kidedu.util.RespJSON;
import com.kidedu.util.SpringContextUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;


public class KOrderAction {

	
   private String chestr;
   private int pageSize;
   private int pageNo;
   private String orderId;
   private String fee;
   private IKOrderService kOrderService=(IKOrderService)SpringContextUtil.getBean("iKOrderService");
   
   private KStudent student;
   
   private  JSONObject json=new JSONObject();
   public String produceOrder()
   {
		HttpServletRequest req = ServletActionContext.getRequest();
		student=(KStudent)req.getSession().getAttribute("STUDENT");
		RespJSON respJSON = new RespJSON();
	   String[] GradeList=chestr.split(",");
	   for(String gradeId : GradeList)
	   {
		 
		          	 
	        	 if(kOrderService.AddNewOrder(gradeId, student.getSid()))
	        			 {
	        		        respJSON.setStatusCode(200);
	        		    	respJSON.setStatusText("OK");
	        			 }     
	   }	  
		respJSON.setData("NO_Datas");
		json = JSONObject.fromObject(respJSON);
	   return Action.SUCCESS;
   }

public String FindMyOrder()
{
	HttpServletRequest req = ServletActionContext.getRequest();
	student=(KStudent)req.getSession().getAttribute("STUDENT");
	RespJSON respJSON = new RespJSON();
	Pagination<KOrderDTO> paginationOrder=kOrderService.MyOrderList(student.getSid(), pageNo, pageSize);
    respJSON.setStatusCode(200);
    respJSON.setStatusText("OK");
    respJSON.setData(paginationOrder);
	json = JSONObject.fromObject(respJSON);
	return Action.SUCCESS;
	
}
public String updateFee()
{
	kOrderService.updatefee(Byte.valueOf(orderId), Float.valueOf(fee));
	 ActionContext.getContext().getSession().put("oid",orderId);
	json.put("Status", "ok");
	return Action.SUCCESS;
}
public String payWx()
{
	
	 String oid=(String)ActionContext.getContext().getSession().get("oid");
	  KOrder order=kOrderService.findOrder(Byte.valueOf(oid));
	  ActionContext.getContext().getSession().put("order",order.getOfee());
	
	return Action.SUCCESS;
}

public String updateOrder()
{
	  KOrder order=kOrderService.findOrder(Byte.valueOf(orderId));
	  order.setOstate(true);
	  kOrderService.updateOrder(order);
    	json.put("Status", "ok");
	return Action.SUCCESS;
}
public String getChestr() {
	return chestr;
}


public void setChestr(String chestr) {
	this.chestr = chestr;
}


public JSONObject getJson() {
	return json;
}


public void setJson(JSONObject json) {
	this.json = json;
}

public int getPageSize() {
	return pageSize;
}

public void setPageSize(int pageSize) {
	this.pageSize = pageSize;
}

public int getPageNo() {
	return pageNo;
}

public void setPageNo(int pageNo) {
	this.pageNo = pageNo;
}

public String getOrderId() {
	return orderId;
}

public void setOrderId(String orderId) {
	this.orderId = orderId;
}

public String getFee() {
	return fee;
}

public void setFee(String fee) {
	this.fee = fee;
}

   
}
